package D12_13_z;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * Created by dingw on 2019/12/11.
 */
public class Demo {
    static Scanner sca = new Scanner(System.in);
    static Scanner sca1 = new Scanner(System.in);
    static Boolean dl = false;
    static long l = 0L;
    static String name1 = "";
    public static void main(String[] args) throws Exception {
        System.out.println("---------------------------欢迎来到大数据商城---------------------------");
        while (true){
            System.out.println("<1>注册");
            System.out.println("<2>登录");
            System.out.println("<3>忘记密码");
            System.out.println("<4>查看商品类别");
            System.out.println("<5>查看商品信息");
            System.out.println("<6>查看商品详细信息");
            System.out.println("<7>购物车");
            int anInt = sca.nextInt();
            if (anInt==1){
                //注册
                zc();
            }
            if (anInt==2){
                //登录
                dl();
            }
            if (anInt==3){
                //忘记密码
                wjpwd();
            }
            if (anInt==4){
                //查看商品类别
                cklb();
            }
            if (anInt==5){
                //查看商品信息
                ckshop();
            }
            if (anInt==6){
                //查询
                cx();
            }
            if (anInt==7){
                //购物车
                gwc();
            }
        }
    }
    public static void zc() throws Exception {
        System.out.println("请输入用户名");
        String name = sca1.nextLine();
        List<User> users = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/user.xml", User.class);
        for (User user : users) {
            if (user.getUsername().equals(name)) {
                System.out.println("用户名已存在，请重新注册");
                return;
            }
        }
        System.out.println("请输入手机号");
        String phone = sca1.nextLine();
        for (User user : users) {
            if (user.getPhone().equals(phone)) {
                System.out.println("该手机号已被注册，请重新选择其他手机号");
                return;
            }
        }
        System.out.println("请输入密码:");
        String pwd = sca1.nextLine();
        System.out.println("请确认密码");
        String pwd1 = sca1.nextLine();
        if (pwd.equals(pwd1)) {
            System.out.println("注册成功!");
            XmlUtils.write("src/Y2019/M12_/D12_13_z/user.xml", new User(users.get(users.size() - 1).getId() + 1, name, pwd, phone, "普通用户", 1000.0), true);
            return;
        }
        System.out.println("输入二次密码有误!!");
    }
    public static boolean dl() throws Exception {
        System.out.println("请输入用户名或手机号:");
        String name = sca1.nextLine();
        System.out.println("请输入密码:");
        String pwd = sca1.nextLine();
        List<User> users = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/user.xml", User.class);
        for (User user : users) {
            if (user.getUsername().equals(name) && user.getPassword().equals(pwd) || user.getPhone().equals(name) && user.getPassword().equals(pwd)) {
                System.out.println("登录成功,大数据商城欢迎您!");
                dl = true;
                l = System.currentTimeMillis();
                name1 = name;
                return true;
            }
        }
        System.out.println("用户名、密码错误，请重新登录!");
        dl = false;
        return false;
    }
    public static void wjpwd() throws Exception {
        System.out.println("请输入忘记密码的用户名");
        String name = sca1.nextLine();
        List<User> users = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/user.xml", User.class);
        for (User user : users) {
            if (user.getUsername().equals(name)) {
                System.out.println("请输入[" + name + "]绑定的手机号:");
                String phone = sca1.nextLine();
                for (User user1 : users) {
                    if (user1.getPhone().equals(phone)) {
                        System.out.println("请输入密码:");
                        String pwd = sca1.nextLine();
                        System.out.println("请确认密码:");
                        String pwd1 = sca1.nextLine();
                        if (pwd.equals(pwd1)) {
                            System.out.println("修改密码成功!");
                            user1.setPassword(pwd);
                            XmlUtils.write("src/Y2019/M12_/D12_13_z/user.xml", users, false);
                            return;
                        }
                        System.out.println("密码输入不一致!");
                        return;
                    }
                }
                System.out.println("手机号输入错误!");
                return;
            }
        }
        System.out.println("没有此用户名!");
    }
    public static void cklb() throws Exception {
        List<Category> categories = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/category.xml", Category.class);
        System.out.println("商品类别id\t商品类别名称");
        for (Category category : categories) {
            System.out.println(category.getId() + "\t\t\t" + category.getCname());
        }
    }
    public static void ckshop() throws Exception {
        List<Product> products = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/product.xml", Product.class);
        System.out.println("id\t商品名称");
        for (Product product : products) {
            System.out.println(product.getId() + "\t" + product.getPname());
        }
    }
    public static void cx() throws Exception {
        List<Category> categories = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/category.xml", Category.class);
        List<Product> products = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/product.xml", Product.class);
        List<ProductDetails> productDetails = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/productDetails.xml", ProductDetails.class);
        System.out.println("请输入商品类别id：");
        String cid = sca1.nextLine();
        if (Integer.parseInt(cid) <= categories.size()) {
            System.out.println("id\t商品名称");
            for (Product product : products) {
                if (product.getCid().equals(cid)) {
                    System.out.println(product.getId() + "\t" + product.getPname());
                }
            }
        } else {
            System.out.println("没有此类别id！");
            return;
        }
        System.out.println("请输入商品id");
        String id = sca1.nextLine();
        if (Integer.parseInt(id) > products.size()) {
            System.out.println("没有此商品id！");
            return;
        }
        String name = "";
        for (Product product : products) {
            if (product.getId().equals(id)) {
                name = product.getPname();
            }
        }
        System.out.println(name + ":");
        System.out.println("商品id\t商品颜色\t    商品内存\t商品价钱\t商品数量");
        for (ProductDetails details : productDetails) {
            if (details.getProductId().equals(id)) {
                System.out.println(details.getId() + "\t\t" + details.getColor() + "\t\t" + details.getMemory() + "\t\t" + details.getPrice() + "\t\t" + details.getNum());
            }
        }
        System.out.println("<1>立即购买\r\n<2>添加购物车");
        String buy = sca1.nextLine();
        if (buy.equals("1")) {
            System.out.println("请输入购买的商品id：");
            String pid = sca1.nextLine();
            System.out.println("请输入购买数量：");
            int num = sca.nextInt();
            ljbuy(name,pid,num);
        }
        if (buy.equals("2")) {
            System.out.println("请输入购买的商品id：");
            String pid = sca1.nextLine();
            System.out.println("请输入购买数量：");
            int num = sca.nextInt();
            addgwc(name,pid,num);
        }
    }
    public static void ljbuy(String name, String id,int num) throws Exception {
        if (dl == false) {
            System.out.println("您还没有登录，用户请登录...");
            return;
        }else if (((System.currentTimeMillis()-l)/1000/60)>=5){
            System.out.println("登录超时,请重新登录!");
            dl = false;
            return;
        }
        List<ProductDetails> productDetails = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/productDetails.xml", ProductDetails.class);
        List<User> users = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/user.xml", User.class);
        for (ProductDetails details : productDetails) {
            if (details.getId().equals(id)) {
                for (User user : users) {
                    if (user.getUsername().equals(name1) && user.getMoney() > num * details.getPrice()) {
                        System.out.println("商品信息\t\t\t\t\t\t\t\t购买数量\t购买总金额");
                        System.out.println(name + ":" + details.getColor() + "\t" + details.getMemory() + "\t" + num + "\t\t" + (details.getPrice() * num));
                        FileUtils.writeStringToFile(new File("src/Y2019/M12_/D12_13_z/orders.txt"),"<"+name1+">"+name + ":" + details.getColor() + "," + details.getMemory() + "," + num + "," + (details.getPrice() * num)+"(待发货)\r\n",true);
                        user.setMoney(user.getMoney()-(details.getPrice() * num));
                        System.out.println("购买成功!");
                        XmlUtils.write("src/Y2019/M12_/D12_13_z/user.xml",users,false);
                        details.setNum(details.getNum()-num);
                        XmlUtils.write("src/Y2019/M12_/D12_13_z/productDetails.xml",productDetails,false);
                        return;
                    }
                }
            }
        }
        System.out.println("金额不足，请选择其他支付方式...");
    }
    public static void gwc() throws IOException {
        while (true){
            System.out.println("---------------购物车---------------");
            System.out.println("<1>修改购物车指定商品数量");
            System.out.println("<2>删除购物车中指定商品");
            System.out.println("<3>结算购物车");
            System.out.println("<4>返回菜单");
            int anInt = sca.nextInt();
            if (anInt == 1){
                edgwc();
            }
            if (anInt == 2){
                if (dl==false){
                    System.out.println("用户请登录...");
                    return;
                }
                else if (((System.currentTimeMillis()-l)/1000/60)>=5){
                    System.out.println("登录超时,请重新登录!");
                    dl = false;
                    return;
                }
                delgwc();
            }
            if (anInt == 3){
                jsgwc();
            }
            if (anInt == 4){
                return;
            }
        }
    }
    public static void addgwc(String name,String id,int num){
        List<ProductDetails> details = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/productDetails.xml", ProductDetails.class);
        List<Cart> carts = new ArrayList<>();
        for (ProductDetails detail : details) {
            if (detail.getId().equals(id)){
                Cart cart = new Cart(id,name,detail.getColor(),detail.getMemory(),num,detail.getPrice());
                carts.add(cart);
                if (new File("src/Y2019/M12_/D12_13_z/cart.xml").isFile()){
                    XmlUtils.write("src/Y2019/M12_/D12_13_z/cart.xml",carts,true);
                    System.out.println("添加成功!");
                    return;
                }
                XmlUtils.write("src/Y2019/M12_/D12_13_z/cart.xml",carts,false);
                System.out.println("添加成功!");
            }
        }
    }
    public static void edgwc(){
        System.out.println("请输入要修改的商品id:");
        String id = sca1.nextLine();
        List<Cart> carts = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/cart.xml", Cart.class);
        List<ProductDetails> details = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/productDetails.xml", ProductDetails.class);
        for (Cart cart : carts) {
            if (cart.getId().equals(id)){
                System.out.println("请输入修改的数量:");
                int anInt = sca.nextInt();
                for (ProductDetails detail : details) {
                    if (detail.getId().equals(id)&&detail.getNum()<anInt){
                        System.out.println("数量不足!");
                        return;
                    }
                }
                cart.setNumber(anInt);
                XmlUtils.write("src/Y2019/M12_/D12_13_z/cart.xml",carts,false);
                System.out.println("修改成功!");
                return;
            }
        }
        System.out.println("没有此商品id!!!");
    }
    public static void delgwc(){
        System.out.println("请输入需要删除的商品id:");
        String id = sca1.nextLine();
        List<Cart> carts = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/cart.xml", Cart.class);
        Iterator<Cart> it = carts.iterator();
        while (it.hasNext()){
            try {
                if (it.next().getId().equals(id)){
                    it.remove();
                    System.out.println("删除成功!");
                    XmlUtils.write("D:\\gzkj\\刷题\\src\\Y2019\\M12_\\D12_13_z\\cart.xml",carts,false);
                    return;
                }
            } catch (Exception e) {
                continue;
            }
        }
        System.out.println("没有该商品!");
    }
    public static void jsgwc() throws IOException {
        List<Cart> carts = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/cart.xml", Cart.class);
        List<User> users = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/user.xml", User.class);
        List<ProductDetails> details = XmlUtils.readAll("src/Y2019/M12_/D12_13_z/productDetails.xml", ProductDetails.class);
        double price = 0;
        for (Cart cart : carts) {
            double v = cart.getNumber() * cart.getPrice();
            System.out.println(cart.getName()+"\t"+cart.getNumber()+"\t"+v);
            price+=v;
        }
        for (User user : users) {
            if (user.getUsername().equals(name1) && user.getMoney()>price){
                for (Cart cart : carts) {
                    System.out.println(cart.getName()+"\t"+cart.getNumber());
                    for (ProductDetails detail : details) {
                        if (detail.getId().equals(cart.getId())){
                            detail.setNum(detail.getNum()-cart.getNumber());
                            FileUtils.writeStringToFile(new File("src/Y2019/M12_/D12_13_z/orders.txt"),"<"+name1+">"+cart.getName() + ":" + detail.getColor() + "," + detail.getMemory() + "," + cart.getNumber() + "," + price+"(待发货)\r\n",true);
                        }
                    }
                }
                System.out.println("总计:"+price);
                user.setMoney(user.getMoney()-price);
                System.out.println("购买成功!");
                XmlUtils.write("src/Y2019/M12_/D12_13_z/user.xml",users,false);
                XmlUtils.write("src/Y2019/M12_/D12_13_z/productDetails.xml",details,false);
                new File("src/Y2019/M12_/D12_13_z/cart.xml").delete();
                return;
            }
        }
        System.out.println("余额不足!");
    }
}
